package com.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;

import java.math.BigDecimal;

/**
 * 支付宝跑龙套
 *
 * @author admin
 * @date 2023/06/19
 */
public class AlipayUtil {

    /**
     * 阿里支付跑龙套
     *
     * @param appId           应用程序id
     * @param price           价格
     * @param payCode         支付代码
     * @param privateKey      私钥
     * @param alipayPublicKey 支付宝公钥
     * @param OrderNumber     订单号
     * @return {@link AlipayTradePayResponse}
     */
    public static AlipayTradePayResponse aliPayUtil(String appId, BigDecimal price, String payCode, String privateKey, String alipayPublicKey, String OrderNumber) {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(alipayConfig);
        } catch (AlipayApiException e) {
            e.getErrCode();
        }
        AlipayTradePayRequest request = new AlipayTradePayRequest();
        AlipayTradePayModel model = new AlipayTradePayModel();
        model.setTotalAmount(price.toString());
        model.setAuthCode(payCode);
        model.setScene("bar_code");
        model.setOutTradeNo(OrderNumber);
        model.setSubject("产品");
        request.setBizModel(model);
        AlipayTradePayResponse response = null;
        try {

            if (alipayClient != null) {
                response = alipayClient.execute(request);
            }
            String body = null;
            if (response != null) {
                body = response.getBody();
            }
            System.out.println(body);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 退款反应
     *
     * @param privateKey      私钥
     * @param alipayPublicKey 支付宝公钥
     * @param appId           应用程序id
     * @param price           价格
     * @param OrderNumber     订单号
     * @return {@link AlipayTradeRefundResponse}
     */
    public static AlipayTradeRefundResponse refundResponse(String privateKey, String alipayPublicKey, String appId, BigDecimal price, String OrderNumber) {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(alipayConfig);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setRefundAmount(String.valueOf(price));
        model.setOutTradeNo(OrderNumber);
        request.setBizModel(model);
        AlipayTradeRefundResponse response = null;
        try {
            assert alipayClient != null;
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        assert response != null;
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return response;
    }

    /**
     * 前创建
     *
     * @param privateKey      私钥
     * @param alipayPublicKey 支付宝公钥
     * @param appId           应用程序id
     * @param price           价格
     * @param OrderNumber     订单号
     * @return {@link AlipayTradePrecreateResponse}
     */
    public static AlipayTradePrecreateResponse preCreate(String privateKey, String alipayPublicKey, String appId, BigDecimal price, String OrderNumber) {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = null;
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        try {
            alipayClient = new DefaultAlipayClient(alipayConfig);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        model.setOutTradeNo(OrderNumber);
        model.setSubject("学费");
        model.setTotalAmount(String.valueOf(price));
        request.setBizModel(model);
        AlipayTradePrecreateResponse response = null;

        try {
            assert alipayClient != null;
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return response;
    }

}
